package org.mango.action;

import com.alibaba.fastjson.JSONObject;
import com.mango.validator.util.ValidUtil;
import org.mango.base.BaseAction;
import org.mango.bean.Grid;
import org.mango.id.Tools;
import org.mango.service.GridService;
import org.mango.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by meigang on 17/9/8.
 */
@RestController
@RequestMapping("/grid")
public class GridAction extends BaseAction{
    @Autowired
    private GridService gridService;
    @Autowired
    private ValidUtil validUtil;

    @GetMapping("/list")
    public String list(HttpServletRequest req){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("creator", UserUtil.getSessionUser(req).getId());
        List<Grid> list = gridService.list(map);
        return JSONObject.toJSONString(list);
    }

    @GetMapping("/listRandom")
    public String listRandom(){
        //全种类，随机前100
        Map<String,Object> map = new HashMap<String,Object>();
        List<Grid> list = gridService.listRandom(map);
        return JSONObject.toJSONString(list);
    }

    @GetMapping("/getById")
    public String getById(String id){
        Grid obj = gridService.getById(id);
        return JSONObject.toJSONString(obj);
    }

    @RequestMapping("/add")
    public String add(Grid obj,HttpServletRequest req){
        obj.setCreateTime(new Date());
        obj.setCreator(UserUtil.getSessionUser(req).getId());
        List<String> msgList = validUtil.validateAll(obj,false,null);
        if(msgList!=null){
            return this.ajaxJson(300,msgList);
        }else {
            obj.setId(Tools.genId()+"");
            gridService.add(obj);
            return this.ajaxJson(200);
        }
    }

    @RequestMapping("/edit")
    public String edit(Grid obj,HttpServletRequest req){
        obj.setLastEditTime(new Date());
        obj.setEditor(UserUtil.getSessionUser(req).getId());
        List<String> msgList = validUtil.validateAll(obj,false,null);
        if(msgList!=null){
            return this.ajaxJson(300,msgList);
        }else {
            gridService.update(obj);
            return this.ajaxJson(200);
        }

    }

    @RequestMapping("/delete")
    public String edit(String id){
        gridService.delete(id);
        return this.ajaxJson(200);
    }



    @RequestMapping("/changeShow")
    public String changeShow(String id,boolean checked){
        String check = "0";
        if(checked){
            check = "1";
        }else{
            check = "0";
        }
        Grid grid = new Grid();
        grid.setId(id);
        grid.setIsShow(check);
        gridService.updateShow(grid);
        return this.ajaxJson(200);
    }
}
